/*
 * Copyright 1999-2004 Alibaba.com All right reserved. This software is the confidential and proprietary information of
 * Alibaba.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with Alibaba.com.
 */
package com.liangdev.common.util;

import com.liangdev.common.constants.CommonConstant;
import com.liangdev.common.constants.InputCheckConstant;
import com.liangdev.common.constants.RegExpConstant;
import com.liangdev.common.enums.UserAccountTypeEnum;

/**
 * 类UserUtil.java的实现描述：TODO 类实现描述
 * 
 * @author chongan.wangca 2012-3-16 下午2:49:46
 */
public class UserUtil {

    /**
     * 判断是否是合法用户名
     * 
     * @param userNick
     * @return
     */
    public static UserAccountTypeEnum isUserLoginName(String loginName, String type) {
        if (loginName.matches(RegExpConstant.USER_ID)) {
            return UserAccountTypeEnum.USER_ID;
        } else if (loginName.matches(RegExpConstant.EMAIL)) {
            return UserAccountTypeEnum.USER_EMAIL;
        } else {
            if (loginName.matches(RegExpConstant.USER_NICK_LETTERS)) {
                loginName = loginName.replaceAll(RegExpConstant.CHINESE_LETTERS, "aa");

                int userNickMin = CommonConstant.USER_NICK_LOGIN.equals(type) ? InputCheckConstant.USER_NICK_MIN_LOGIN : InputCheckConstant.USER_NICK_MIN_REG;
                if (loginName.length() >= userNickMin && loginName.length() <= InputCheckConstant.USER_NICK_MAX
                    && loginName.indexOf("__") == -1) {
                    return UserAccountTypeEnum.USER_NICK;
                }

            }
        }
        return null;
    }
}
